<template>
  <d2-container>
    <div class="flex-1">
      <div class="flex-1 ml-2 mt-3">
        <div class="flex-1 row-ac">
          <div class="row-ac">
            <div class="font-s-2 text-light-grey mr-1">按商家查询</div>
            <div>
              <el-input size="mini" v-model="queryInfo.merchant_id" placeholder="商家查询"></el-input>
            </div>
          </div>
          <div class="row-ac ml-5">
            <div class="font-s-2 text-light-grey mr-1">按优惠状态</div>
            <div>
              <el-input size="mini" v-model="queryInfo.merchant_id" placeholder="优惠状态"></el-input>
            </div>
          </div>
          <div class="ml-2">
            <el-button size="mini" type="primary" @click="clickQuery">查询</el-button>
          </div>
        </div>
      </div>
      <div class="flex-1 mt-2">
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="全部优惠券" name="first">
            <el-table :data="couponList" stripe size="medium">
              <el-table-column type="expand" width="20">
                <template slot-scope="props">
                  <div class="flex-1 mx-3">
                    <el-form label-position="left" inline class="demo-table-expand">
                      <el-form-item label="优惠券名称">
                        <span>{{ props.row.title }}</span>
                      </el-form-item>
                      <el-form-item label="审核状态">
                        <el-tag size="small" v-if="props.row.coupon_examine == 0" type="danger" effect="dark">未审核
                        </el-tag>
                        <el-tag size="small" v-if="props.row.coupon_examine == 1" type="success"
                          effect="dark">审核通过</el-tag>
                        <el-tag size="small" v-if="props.row.coupon_examine == 2" type="danger" effect="dark">未通过
                        </el-tag>
                      </el-form-item>
                      <el-form-item label="开始时间">
                        <span>{{ props.row.start_datetime }}</span>
                      </el-form-item>
                      <el-form-item label="过期时间">
                        <span>{{ props.row.end_datetime }}</span>
                      </el-form-item>
                    </el-form>
                  </div>
                </template>
              </el-table-column>
              <el-table-column align="center" label="序号" type="index" width="20" />
              <el-table-column label="优惠券名称" prop="title" width="80" />
              <el-table-column align="center" label="发放数量" prop="coupon_count" width="50" />
              <el-table-column align="center" label="剩余数量" prop="coupon_residue" width="50" />
              <el-table-column align="center" label="优惠金额" prop="discount_amount" width="50" />
              <el-table-column align="center" label="使用金额" prop="minimum_order_amount" width="50" />
              <el-table-column align="center" label="状态" prop="coupon_examine" width="60">
                <template slot-scope="scope">
                  <el-tag size="small" v-if="scope.row.coupon_status == 0" type="success" effect="dark">正常 </el-tag>
                  <el-tag size="small" v-if="scope.row.coupon_status == 1" type="danger" effect="dark">下架</el-tag>
                  <el-tag size="small" v-if="scope.row.coupon_status == 2" type="danger" effect="dark">领完</el-tag>
                  <el-tag size="small" v-if="scope.row.coupon_status == 3" type="danger" effect="dark">过期</el-tag>
                </template>
              </el-table-column>
              <el-table-column align="center" label="操作" width="140">
                <template slot-scope="scope">
                  <el-button size="mini" v-if="scope.row.coupon_examine == 1">使用详情</el-button>
                  <el-button size="mini" @click="openPatch(scope.row.id)" type="success"
                    v-if="scope.row.coupon_examine == 0">审核</el-button>
                  <el-button size="mini" @click="onClick(scope.row.id)" type="primary">修改</el-button>
                  <el-button size="mini" @click="onDelete(scope.row.id)" type="danger">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="已审核" name="second">
            <el-table :data="couponList" stripe size="medium">
              <el-table-column align="center" label="序号" type="index" width="45" />
              <el-table-column label="优惠券名称" prop="title" width="80" />
              <el-table-column align="center" label="发放数量" prop="coupon_count" width="50" />
              <el-table-column align="center" label="剩余数量" prop="coupon_residue" width="50" />
              <el-table-column align="center" label="优惠金额" prop="discount_amount" width="50" />
              <el-table-column align="center" label="使用金额" prop="minimum_order_amount" width="50" />
              <el-table-column align="center" label="状态" prop="coupon_examine" width="60">
                <template slot-scope="scope">
                  <el-tag size="small" v-if="scope.row.coupon_status == 0" type="success" effect="dark">正常 </el-tag>
                  <el-tag size="small" v-if="scope.row.coupon_status == 1" type="danger" effect="dark">下架</el-tag>
                  <el-tag size="small" v-if="scope.row.coupon_status == 2" type="danger" effect="dark">领完</el-tag>
                  <el-tag size="small" v-if="scope.row.coupon_status == 3" type="danger" effect="dark">过期</el-tag>
                </template>
              </el-table-column>
              <el-table-column align="center" label="操作" width="100">
                <template slot-scope="scope">
                  <el-button size="mini">使用详情</el-button>
                  <el-button size="mini" @click="onClick(scope.row.id)">修改</el-button>
                  <el-button size="mini" @click="onDelete(scope.row.id)" type="danger">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="未审核" name="third">
            <el-table :data="couponList" stripe size="medium">
              <el-table-column align="center" label="序号" type="index" width="30" />
              <el-table-column label="优惠券名称" prop="title" width="80" />
              <el-table-column align="center" label="发放数量" prop="coupon_count" width="50" />
              <el-table-column align="center" label="剩余数量" prop="coupon_residue" width="50" />
              <el-table-column align="center" label="优惠金额" prop="discount_amount" width="50" />
              <el-table-column align="center" label="使用金额" prop="minimum_order_amount" width="50" />
              <el-table-column align="center" label="审核状态" prop="coupon_examine" width="50">
                <template slot-scope="scope">
                  <el-tag size="small" type="danger" effect="dark">未审核 </el-tag>
                </template>
              </el-table-column>
              <el-table-column align="center" label="状态" prop="coupon_examine" width="60">
                <template slot-scope="scope">
                  <el-tag size="small" v-if="scope.row.coupon_status == 0" type="success" effect="dark">正常 </el-tag>
                  <el-tag size="small" v-if="scope.row.coupon_status == 1" type="danger" effect="dark">下架</el-tag>
                  <el-tag size="small" v-if="scope.row.coupon_status == 2" type="danger" effect="dark">领完</el-tag>
                  <el-tag size="small" v-if="scope.row.coupon_status == 3" type="danger" effect="dark">过期</el-tag>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="未通过" name="fourth">
            <el-table :data="couponList" stripe size="medium">
              <el-table-column align="center" label="序号" type="index" width="30" />
              <el-table-column label="优惠券名称" prop="title" width="80" />
              <el-table-column align="center" label="发放数量" prop="coupon_count" width="50" />
              <el-table-column align="center" label="剩余数量" prop="coupon_residue" width="50" />
              <el-table-column align="center" label="优惠金额" prop="discount_amount" width="50" />
              <el-table-column align="center" label="使用金额" prop="minimum_order_amount" width="50" />
              <el-table-column align="center" label="审核状态" prop="coupon_examine" width="50">
                <template slot-scope="scope">
                  <el-tag size="small" type="danger" effect="dark">未通过</el-tag>
                </template>
              </el-table-column>
              <el-table-column align="center" label="操作" width="140">
                <template slot-scope="scope">
                  <el-button size="mini" @click="openPatch(scope.row.id)" type="success">重新审核</el-button>
                  <el-button size="mini" @click="onClick(scope.row.id)" type="primary">修改</el-button>
                  <el-button size="mini" @click="onDelete(scope.row.id)" type="danger">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="flex-1 ml-2 mt-2">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="couponTotal">
        </el-pagination>
      </div>
    </div>
  </d2-container>
</template>

<script>
  import {
    couponListUrl,
    couponDeleteUrl,
    couponPatchUrl
  } from '@/utils/promotion'
  export default {
    data() {
      return {
        queryInfo: {
          coupon_examine: '',
          merchant_id: '',
          page: 1,
          page_size: 10
        },
        auditStatus: {
          coupon_examine: ''
        },
        couponList: [],
        activeName: 'first',
        couponTotal: 0
      }
    },
    created() {
      this.init()
    },
    methods: {
      init() {
        this.getCouponList()
      },
      handleClick(tab, event) {
        if (tab.paneName == 'first') {
          this.queryInfo.coupon_examine = ''
        } else if (tab.paneName == 'second') {
          this.queryInfo.coupon_examine = 1
        } else if (tab.paneName == 'third') {
          this.queryInfo.coupon_examine = 0
        } else if (tab.paneName == 'fourth') {
          this.queryInfo.coupon_examine = 2
        }
        this.getCouponList()
      },
      getCouponList() {
        this.$get(couponListUrl, this.queryInfo).then(res => {
          this.couponList = res.data.data.list
          this.couponTotal = res.data.data.total
        })
      },
      clickQuery() {
        this.getCouponList()
      },
      onClick(couponId) {
        this.$router.push({
          path: '/coupon/addCoupon',
          query: {
            couponId: couponId // 添加第二个参数
          }
        })
      },
      onDelete(couponId) {
        this.$confirm('此操作将删除该类目?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$del(couponDeleteUrl + couponId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'danger',
                message: '删除成功!'
              });
              this.getCouponList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      openPatch(couponId) {
        this.$confirm('该店铺是否通过审核?', '提示', {
          confirmButtonText: '通过',
          cancelButtonText: '拒绝',
          type: 'warning'
        }).then(() => {
          this.auditStatus.coupon_examine = 1
          this.$patch(couponPatchUrl + couponId + '/', this.auditStatus).then(res => {
            this.getCouponList()
            this.$message({
              type: 'success',
              message: '审核成功'
            })
          })
        }).catch(() => {
          this.auditStatus.coupon_examine = 2
          this.$patch(couponPatchUrl + couponId + '/', this.auditStatus).then(res => {
            this.getCouponList()
            this.$message({
              type: 'danger',
              message: '拒绝通过'
            })
          })
        });
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.page_size = newSize
        this.getCouponList()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getCouponList()
      },
    }
  }
</script>

<style>
</style>